package yjpay.service.impl;

import com.alibaba.fastjson.JSON;
import com.util.common.RandomUtil;
import com.util.common.YJPayUtil;
import com.util.encrypt.AES;
import com.util.encrypt.EncryUtil;
import com.util.encrypt.RSA;
import org.springframework.stereotype.Service;
import yjpay.URIList.PayAPIURIList;
import yjpay.service.PayAPIService;

import java.util.ResourceBundle;
import java.util.TreeMap;

@Service("payAPIService")
public class PayAPIServiceImpl implements PayAPIService {

	private ResourceBundle resb1 = ResourceBundle.getBundle("payapi");

	// 从配置文件读取易宝分配的公钥
	private String yibaoPublicKey = resb1
			.getString("payapi.paytest_yibao_publickey");

	// 从配置文件读取商户自己的私钥
	private String merchantPrivateKey = resb1
			.getString("payapi.paytest_merchant_privatekey");

	// 商户自己随机生成的AESkey
	private String merchantAesKey = RandomUtil.getRandom(16);

	// 商户账户编号
	private String merchantaccount = resb1
			.getString("payapi.paytest_merchantaccount");

	// 从配置文件读取支付API接口URL前缀
	private String urlPrefix = resb1.getString("payapi.pay_urlprefix");

	@Override
	public String bindCardPayRequest(int amount, String bindid, int currency,
			String identityid, int identitytype, String orderid, String other,
			String productcatalog, String productdesc, String productname,
			Integer transtime, String userip, String callbackurl,
			String fcallbackurl, int terminaltype, String terminalid)
			throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_BINDCARDPAY.getValue();
		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("amount", amount);
		map.put("bindid", bindid);
		if (currency > 0) {
			map.put("currency", currency);
		}
		map.put("identityid", identityid);
		map.put("identitytype", identitytype);
		map.put("orderid", orderid);
		if (other != null) {
			map.put("other", other);
		}
		map.put("productcatalog", productcatalog);
		if (productdesc != null) {
			map.put("productdesc", productdesc);
		}
		map.put("productname", productname);
		map.put("transtime", transtime);
		map.put("userip", userip);
		if (callbackurl != null) {
			map.put("callbackurl", callbackurl);
		}
		if (fcallbackurl != null) {
			map.put("fcallbackurl", fcallbackurl);
		}
		map.put("terminaltype", terminaltype);
		map.put("terminalid", terminalid);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		// 生成data
		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);

	}

	@Override
	public String unbindCard(String bindid, String identityid, int identitytype)
			throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_UNBINDCARD.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("identityid", identityid);
		map.put("bindid", bindid);
		map.put("identitytype", identitytype);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		// 生成data
		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);

	}

	@Override
	public String queryPay(String orderid) throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_QUERYORDER.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("orderid", orderid);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, false);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);

	}

	@Override
	public String bankCardCheck(String cardno) throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_BANKCARDCHECK.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("cardno", cardno);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);

	}

	@Override
	public String bindList(String identityid, int identitytype)
			throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_BINDLIST.getValue();

		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("identityid", identityid);
		map.put("identitytype", identitytype);

		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, false);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);
	}

	@Override
	public String sendvalideCode(String orderid) throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_SENDVALIDATECODE.getValue();
		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("orderid", orderid);
		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);
	}

	@Override
	public String confirmPay(String orderid, String validecode)
			throws Exception {
		// 完整的请求地址
		String requestURL = urlPrefix
				+ PayAPIURIList.PAYAPI_CONFIRMPAY.getValue();
		TreeMap<String, Object> map = new TreeMap<String, Object>();
		map.put("merchantaccount", merchantaccount);
		map.put("orderid", orderid);
		if (validecode != null) {
			if (!validecode.trim().equals("")) {
				map.put("validatecode", validecode);
			}
		}
		// 生成RSA签名
		String sign = EncryUtil.handleRSA(map, merchantPrivateKey);

		map.put("sign", sign);

		String info = JSON.toJSONString(map);
		String data = AES.encryptToBase64(info, merchantAesKey);

		// 使用RSA算法将商户自己随机生成的AESkey加密
		String encryptkey = RSA.encrypt(merchantAesKey, yibaoPublicKey);

		YJPayUtil yjpayutil = new YJPayUtil();

		// 请求一键支付接口
		String ybresult = yjpayutil.payAPIRequest(requestURL, data, encryptkey,
				merchantaccount, true);

		// 将一键支付返回的结果进行验签，解密并返回
		return yjpayutil.checkYbResult(ybresult);
	}

}
